import { Prop } from 'vue-property-decorator';
import {
  PanelDataProvider,
  PanelLayoutComponentProvider,
  PanelLayoutDataProvider,
  PanelLayoutProvider,
} from '../../interface';
import { PanelController } from '../../panel-controller';
import { BasicComponent } from '../basic-compoent/basic-compoent';

/**
 * 布局基础组件
 *
 * @author chitanda
 * @date 2022-02-17 17:02:48
 * @export
 * @class LayoutItemBasicComponent
 * @extends {BasicComponent}
 * @implements {PanelLayoutComponentProvider}
 */
export class LayoutItemBasicComponent extends BasicComponent implements PanelLayoutComponentProvider {
  @Prop()
  panel!: PanelController;

  @Prop()
  data!: PanelLayoutDataProvider;

  children: PanelDataProvider[] = [];

  created(): void {
    this.load();
  }

  async load(): Promise<void> {
    const items = await this.panel.fetchDataItems(this.data ? this.data.id : '');
    if (items && items.length > 0) {
      this.children = items;
    }
  }

  /**
   * 删除数据
   *
   * @author chitanda
   * @date 2022-02-18 16:02:49
   * @param {PanelDataProvider} data
   * @return {*}  {Promise<boolean>}
   */
  deleteItem(data: PanelDataProvider): Promise<boolean> {
    const provider = this.panel.getProvider(this.data.type) as PanelLayoutProvider;
    return provider.removeItem(data);
  }
}
